#include <iostream>
#include <string>

// 适配器模式
// Adapter：适配器模式将一个类的接口转换成客户希望的另外一个接口，使得原本由于接口不兼容而不能一起工作的那些类可以一起工作

class Person
{
protected:
    std::string _name;

public:
    Person(std::string name)
    {
        _name = name;
    }
    virtual ~Person() {}
    virtual void chifan() = 0;
    virtual void shuohua() = 0;
};

class SiChuan : public Person
{
private:
    /* data */
public:
    SiChuan(std::string name) : Person(name) {}
    ~SiChuan() {}
    virtual void chifan()
    {
        std::cout << _name << " 吃米饭\n";
    }
    virtual void shuohua()
    {
        std::cout << _name << " 说四川话\n";
    }
};

class DongBei : public Person
{
private:
    /* data */
public:
    DongBei(std::string name) : Person(name) {}
    ~DongBei() {}
    virtual void chifan()
    {
        std::cout << _name << " 吃饺子\n";
    }
    virtual void shuohua()
    {
        std::cout << _name << " 说东北话\n";
    }
};

// 外国人的方式和我们的方式不一样，这时需要把方式转换成我们的方式，引入适配器类
class Foreigner
{
public:
    Foreigner(std::string name)
    {
        this->_name = name;
    }

    void eat()
    {
        std::cout << _name << " eat bread\n";
    }

    void speak()
    {
        std::cout << _name << " speak english\n";
    }

private:
    std::string _name;
};

// 适配器
// 将Foreigner类方法，转化为Person接口
class Translator : public Person
{
private:
    Foreigner *_foreigner;

public:
    // 代理
    Translator(std::string name) : Person(name)
    {
        _foreigner = new Foreigner(name);
    }

    ~Translator()
    {
        if (_foreigner)
        {
            delete _foreigner;
        }
    }

    void chifan()
    {
        _foreigner->eat();
    }

    void shuohua()
    {
        _foreigner->speak();
    }
};

int main(int argc, char const *argv[])
{
    Person *b = new DongBei("张三");
    b->shuohua();
    b->chifan();
    

    Person *b1 = new SiChuan("李四");
    b1->shuohua();
    b1->chifan();
    

    Person *b2 = new Translator("john");
    b2->shuohua();
    b2->chifan();
    
    return 0;
}
